<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" @submit.native.prevent @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input placeholder="请验编号" v-model="dataForm.queryCheckId" :clearable="true" type="number" size="mini">
          <template slot="prepend">请验编号</template>
        </el-input>
      </el-form-item>
      <br />
      <el-form-item>
        <el-input placeholder="品名" v-model="dataForm.productName" :clearable="true" size="mini">
          <template slot="prepend">品名</template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-input placeholder="GMP编码" v-model="dataForm.productGmp" :clearable="true" size="mini">
          <template slot="prepend">GMP编码</template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-input placeholder="物料编码" v-model="dataForm.batchNum" :clearable="true" size="mini">
          <template slot="prepend">物料编码</template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-select v-model="dataForm.status" clearable placeholder="全部状态" size="mini">
          <el-option label="待处理" value="0" />
          <el-option label="未取样" value="1" />
          <el-option label="已取样" value="2,3,4,5,6,7,8,10,11" />
        </el-select>
      </el-form-item>

      <el-form-item v-if="userInfo.types.length != 0">
        <el-select v-model="dataForm.checkTypeId" clearable placeholder="全部分类" size="mini">
          <el-option v-for="t in userInfo.types" :key="t.checkTypeId" :label="t.checkTypeName" :value="t.checkTypeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" class="pan-btn primary-btn" @click="searchData()" icon="el-icon-search" size="mini">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" class="pan-btn danger-btn" v-if="isAuth('check:get')" @click="get()" icon="el-icon-shopping-bag-2"
          :disabled="selectionList.length == 0" size="mini">取样</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="dataList" v-loading="dataListLoading" border fit highlight-current-row @selection-change="selectionChange"
      style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="checkId" header-align="center" align="center" label="编号" width="100">
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态" width="100">
       <template slot-scope="{row}">
         <el-tag :type="row.status < 2 ? 'danger' : 'success' " size="small">
           {{ row.status == 0 ? '待处理' : row.status < 2 ? '未取样' : '已取样' }}
         </el-tag>
       </template>
      </el-table-column>
      <el-table-column prop="checkTypeName" header-align="center" align="center" label="分类">
      </el-table-column>
      <el-table-column prop="productName" header-align="center" align="center" label="品名">
      </el-table-column>
      <el-table-column prop="batchNum" header-align="center" align="center" label="物料编码">
      </el-table-column>
      <el-table-column prop="productSpec" header-align="center" align="center" label="货品规格">
        <template slot-scope="{row}">
          <span v-html="row.productSpec"></span>
        </template>
      </el-table-column>
      <el-table-column prop="productPackSpec" header-align="center" align="center" label="包装规格">
      </el-table-column>
      <el-table-column prop="registCheckDepartmentName" header-align="center" align="center" label="请验部门">
      </el-table-column>
      <el-table-column prop="registCheckUserName" header-align="center" align="center" label="请验人">
      </el-table-column>
      <el-table-column prop="registCheckDate" header-align="center" align="center" label="请验时间">
        <template slot-scope="{row}">{{row.registCheckDate | changeTime}}</template>
      </el-table-column>
      <el-table-column prop="getQuantity" header-align="center" align="center" label="取样量">
        <template slot-scope="{row}">{{row.getQuantity == null ? null : row.getQuantity + row.getUnitName}}</template>
      </el-table-column>
      <el-table-column prop="getUserName" header-align="center" align="center" label="取样人">
      </el-table-column>
      <el-table-column prop="getDate" header-align="center" align="center" label="取样时间">
        <template slot-scope="{row}">{{row.getDate | changeTime}}</template>
      </el-table-column>
      <el-table-column v-if="dataForm.checkTypeId == 6" prop="stabilityModeName" header-align="center" align="center"
        label="考察方式">
        <template slot-scope="{row}">
          {{ row.stabilityModeName + ' ' + row.timeValue + row.timeName }}
        </template>
      </el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注">
      </el-table-column>
      <el-table-column header-align="center" align="center" label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="danger" class="pan-btn danger-btn" v-if="scope.row.status == 0 && scope.row.checkReturn.toUserId == userInfo.userId"
            size="mini" @click="handle(scope.row)">处理</el-button>
          <el-button type="danger" class="pan-btn danger-btn" v-if="scope.row.status == 1 && isAuth('check:get')" size="mini"
            @click="get(scope.row)">取样</el-button>
          <el-button type="success" class="pan-btn success-btn" v-if="scope.row.status >= 2" size="mini" @click="printlabel(scope.row,'样品标签')">打印样品标签</el-button>
          <el-button type="success" class="pan-btn success-btn" v-if="scope.row.status >= 2" size="mini" @click="printlabel(scope.row,'取样证')">打印取样证</el-button>

<!--        <el-button type="success" class="pan-btn success-btn" size="mini"
        v-if="scope.row.status >= 2 && scope.row.checkTypeId == 1"
        @dblclick.native="printHistory(scope.row,'download')" @click="printHistory(scope.row,'view')" >取样记录</el-button>

        <el-button type="success" class="pan-btn success-btn" size="mini"
        v-if="scope.row.status >= 2 && scope.row.checkTypeId == 2"
        @dblclick.native="printHistory(scope.row,'download')" @click="printHistory(scope.row,'view')" >取样记录</el-button>

        <el-button type="success" class="pan-btn success-btn" size="mini"
        v-if="scope.row.status >= 2 && scope.row.checkTypeId == 3"
        @dblclick.native="printHistory(scope.row,'download')" @click="printHistory(scope.row,'view')" >取样记录</el-button> -->

        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="详情" width="100">
        <template slot-scope="scope">
          <el-button type="info" class="pan-btn info-btn" size="mini" @click="info(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>

    <get v-if="getVisible" @close="close('getVisible')" @refreshData="getDataList" :childDataForm="childDataForm"
      :returnInfo="returnInfo" />

    <samplelabel v-if="labelVisible" @close="close('labelVisible')" :childDataForm="childDataForm" />
    <samplecard v-if="cardVisible" @close="close('cardVisible')" :childDataForm="childDataForm" />

    <check-info v-if="infoVisible" @close="close('infoVisible')" @refreshData="getDataList" :childDataForm="childDataForm" />

  </div>
</template>

<script>
  import {
    mapGetters
  } from 'vuex'
  import CommonMethod from '@/views/mixin/common'
  import {
    CODE_OK
  } from '@/common/js/config'
  import {
    checkList
  } from '@/api/check'

  // import {
  //   printSamplingHistory
  // } from '@/api/print'

  import Get from '@/views/modules/process/get/get'
  import CheckInfo from '@/views/modules/process/common/check-info'
  import samplelabel from '@/views/modules/printmodel/printchoice/label/samplelabel'
  import samplecard from '@/views/modules/printmodel/printchoice/label/samplecard'
  export default {
    filters: {
      statusFilter(status) {
        const statusMap = {
          0: '待处理',
          1: '待取样',
          2: '待收样',
          3: '待分发',
          4: '检验中',
          5: '检验中',
          6: '待结论',
          10: '待放行',
          7: '待放行',
          8: '待确认',
          11: '已完成'
        }
        return statusMap[status]
      }
    },
    mixins: [CommonMethod],
    components: {
      Get,
      CheckInfo,
      samplelabel,
      samplecard
    },
    data() {
      return {
        currentStatus: 1,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataForm: {
          status: '1'
        },
        childDataForm: '',
        returnInfo: null,
        selectionList: [],
        getVisible: false,
        infoVisible: false,
        printVisible: false,
        printVisibles: false,
        labelVisible:false,
        cardVisible:false
      }
    },
    computed: {
      ...mapGetters([
        'userInfo'
      ])
    },
    mounted() {
      this.getInfo()
    },
    methods: {
      printlabel(row,tag){
        this.childDataForm = JSON.parse(JSON.stringify(row));
        if("样品标签" == tag){
          this.labelVisible = true;
        } else
        if("取样证" == tag){
          this.cardVisible = true;
        }
      },
      printHistory(row,tag){
        this.historyButton = true;
        //封装打印数据
        let params = {
          checkId:row.checkId,
          checkType:row.checkTypeId,
          getDate:row.getDate,
          productName:row.productName,
          productGmp:row.productGmp,
          spec:row.productSpec,
          batchNum:row.batchNum,
          pack:row.productPackSpec
        }
        // printSamplingHistory(params).then(res=>{
        //   if (res.code === CODE_OK) {
        //     if(tag == 'down'){
        //           window.open(res.downloadUrl, '_blank')
        //     }else{
        //           window.open(res.viewUrl, '_blank')
        //     }
        //   }
        // }).catch();
        this.historyButton = false;
      },
      print(row) {
        this.childDataForm = JSON.parse(JSON.stringify(row))
        this.printVisible = true
      },
      prints(row) {
        this.childDataForm = JSON.parse(JSON.stringify(row))
        this.printVisibles = true
      },
      searchData() {
        this.pageIndex = 1
        this.getDataList()
      },
      getInfo() {
        this.getDataList()
      },
      getDataList() {
        this.dataListLoading = true
        const params = {
          page: this.pageIndex,
          limit: this.pageSize,
          status: this.dataForm.status || '0,1,2,3,4,5,6,10,7,8,11',
          checkTypeId: this.dataForm.checkTypeId || this.userInfo.types.map(t => {
            return t.checkTypeId
          }).join(','),
          productName: this.dataForm.productName || null,
          productGmp: this.dataForm.productGmp || null,
          batchNum: this.dataForm.batchNum || null,
          checkId: this.dataForm.queryCheckId || null
        }
        checkList(params).then(res => {
          if (res.code === CODE_OK) {
            this.dataList = res.page.list
            this.totalPage = res.page.totalCount
          } else {
            this.$message.error(res.msg)
          }
          this.dataListLoading = false
        }).catch()
      },
      handle(row) {
        let returnInfo = '退回理由：' + row.checkReturn.returnReason + '</br>' + row.checkReturn.fromUserName + '在 ' + this.statusMap(
          row.checkReturn.fromStatus) + ' 时退回了您的请验单  ' + '</br>' + row.checkReturn.returnTime
        this.$confirm(returnInfo, '提示', {
          dangerouslyUseHTMLString: true,
          confirmButtonText: '去处理',
          cancelButtonText: '再想想',
          type: 'warning'
        }).then(() => {
          let arr = []
          arr.push(row)
          this.childDataForm = arr
          this.returnInfo = row.checkReturn
          this.getVisible = true
        }).catch(() => {})
      },
      get(row) {
        this.childDataForm = row ? [row] : this.selectionList
        this.getVisible = !this.getVisible
      },
      info(row) {
        this.childDataForm = {
          currentStatus: this.currentStatus,
          info: row
        }
        this.infoVisible = !this.infoVisible
      },
      close(val) {
        this.childDataForm = ''
        this[val] = !this[val]
      },
      selectionChange(items) {
        this.selectionList = items.filter(item => {
          return item.status == 1
        })
      },
      statusMap(status) {
        const map = {
          0: '请验',
          1: '取样',
          2: '收样',
          3: '分样',
          4: '检验',
          5: '检验',
          6: '结论',
          10: '放行',
          7: '放行'
        }
        return map[status]
      }
    }
  }
</script>

<style scoped>
  >>>.el-form-item__content {
    line-height: normal;
  }
</style>
